package edu.asu.sefcom.extract.profile;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import edu.asu.sefcom.extract.profile.data.Neo4jConnector;

public class ParserProfileDriver {

//	public static void main(String args[]) throws Exception {
//	SimpleDateFormat format1=new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
//	System.out.println(format1.parse("Sun Jun 08 00:00:00 MST 2008"));
//	}
	
	public static void main(String args[]) throws FileNotFoundException {
		
		String property1="edu.asu.sefcom.extract.profile.userfileinfo";
		String classToUse="userProfileParserTemplate";
		
//		String property1="edu.asu.sefcom.extract.profile.groupfileinfo";
//		String classToUse="commProfileParserTemplate";
		
		String[] filearr = null;

		ApplicationContext beanFactory = new ClassPathXmlApplicationContext(
				"applicationConfig.xml");
		Map<String, String[]> map1 = new HashMap<String, String[]>();

		map1 = getFileInfo(property1);
		try{
		for (String startD : map1.keySet()) {
			File startDir = new File(startD);
			filearr = map1.get(startD);

			for (String userName : filearr) {
				List<File> listoffiles = FileListing.getFileListingForUser(
						startDir, userName);
				int working = 0;
				Logger.getLogger(UserProfileParserTemplate.class).info(
						"----" + listoffiles.size());
				List<String> targetL = new LinkedList<String>();
				Pattern p = Pattern.compile(".*profile.*.htm");
				for (File listF : listoffiles) {
					Matcher m = p.matcher(listF.getName());
					boolean result = m.find();
					if (result && listF.getName().endsWith(".htm")) {
						targetL.add(listF.getAbsolutePath());
					}
				}
				Logger.getLogger(UserProfileParserTemplate.class).warn(
						"list of files " + targetL);
				Parser parserClass = (Parser) beanFactory
						.getBean(classToUse);

				String fileN = "";
				Parser[] parsers = { parserClass };
				for (Parser parser : parsers) {
					try {
						fileN = targetL.get(targetL.size() - 1);
						Logger.getLogger(UserProfileParserTemplate.class)
								.warn(targetL.size()
										+ "------------------"
										+ fileN
										+ "--------------------------------------------------");

						parser.setUserName(userName);
						parser.parseData(fileN);
						working++;
						Logger.getLogger(UserProfileParserTemplate.class)
								.warn("processed user number "
										+ working
										+ "++++++++++++++++++++++++++++++++++++++++++++++++++++: "
										+ fileN);
					} catch (Exception e) {
						System.out.println(e + fileN);
						e.printStackTrace();
					}
				}
			}
		}
		}finally{
		Neo4jConnector connector = (Neo4jConnector) beanFactory
				.getBean("connector");
		connector.shutdownDB();
		}
	}

	private static Map<String, String[]> getFileInfo(String property) {
		Map<String, String[]> ret = new HashMap<String, String[]>();
		try {
			Properties properties=new Properties();
			properties.load(new FileReader("config/app.properties"));
			String userFileInfo=properties.getProperty(property);
			
			File file = new File(userFileInfo);
			FileReader fileReader = new FileReader(file);
			BufferedReader br = new BufferedReader(fileReader);
			String userName = "";
			while ((userName = br.readLine()) != null) {
				String[] arr1 = userName.split("=");
				String key = arr1[0];
				String commSepV = arr1[1];
				String[] value = commSepV.split(",");
				ret.put(key, value);
			}
			br.close();
			fileReader.close();
		} catch (Exception e) {
			throw new IllegalArgumentException(e);
		}
		return ret;
	}

}
